package com.dubbo.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dubbo.server.service.map.QuanZiServiceMapp;
import com.dubbo.server.vo.MessageCommentsVo;
import com.dubbo.server.vo.PageResult;
import com.tanhua.common.pojo.Comment;
import com.tanhua.common.pojo.PageInfo;
import com.tanhua.common.pojo.UserInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessageCommentsService {
//评论逻辑
    @Reference
    private QuanZiServiceMapp quanZiApi;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询评论列表
     *
     * @param messageID
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult queryCommentList(String messageID, Integer page, Integer pageSize) {

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);

        //查询评论列表数据
        PageInfo<Comment> pageInfo = messageCommentList(messageID, page, pageSize);
        List<Comment> records = pageInfo.getRecords();
        if (CollUtil.isEmpty(records)) {
            return pageResult;
        }
        //查询用户信息
        List<Object> userIdList = CollUtil.getFieldValues(records, "userId");
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();

        List<UserInfo> userInfoList = (List<UserInfo>) queryWrapper.in("user_id", userIdList);

        List<MessageCommentsVo> messageList = new ArrayList<>();
        for (Comment record : records) {
            MessageCommentsVo messageCommentsVo = new MessageCommentsVo();
            //评论人id
            messageCommentsVo.setUserId(Convert.toInt(record.getUserId()));
            //评论内容
            messageCommentsVo.setContent(record.getContent());
            //编号
            messageCommentsVo.setId(record.getId().toHexString());
            //评论时间
            messageCommentsVo.setCreateDate(Integer.valueOf(DateUtil.format(new Date(record.getCreated()),"YYYY-MM-dd HH:mm")));
            //点赞数
            messageCommentsVo.setLikeCount(23);//Convert.toInt(this.quanZiApi.queryLikeCount(messageCommentsVo.getId()))
            //评论数
            messageCommentsVo.setCounts(12);//Convert.toInt(this.quanZiApi.queryCommentCount(messageCommentsVo.getId()))
            //填充用户信息
            for (UserInfo userInfo : userInfoList) {
                if (ObjectUtil.equals(messageCommentsVo.getUserId(), userInfo.getUserId())) {
                    //评论人昵称
                    messageCommentsVo.setNickname(userInfo.getNickName());
                    break;
                }
            }
            messageList.add(messageCommentsVo);
        }
        pageResult.setItems(messageList);

        return pageResult;
    }



    /**
     * 查询评论
     *
     * @return
     */
    public PageInfo<Comment> messageCommentList(String messageID, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("created")));

        Query query = Query.query(Criteria.where("userId").is(new ObjectId(messageID))).with(pageRequest);
        List<Comment> commentList = this.mongoTemplate.find(query, Comment.class);
        Integer counts = Math.toIntExact(this.mongoTemplate.count(query, Comment.class));

        PageInfo<Comment> pageInfo = new PageInfo<>();
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(page);
        pageInfo.setRecords(commentList);
        pageInfo.setTotal(counts);

        return pageInfo;
    }

}
